package com.bestom.waterquality.logic;

import android.content.Context;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bestom.waterquality.R;
import com.bestom.waterquality.WaterQualityApplication;
import com.bestom.waterquality.entity.Constant;
import com.bestom.waterquality.util.SharedPreferencesUtil;
import com.bestom.waterquality.util.ToastUtil;

/**
 * 传感器设置
 */
public class SensorSet {

    private Context mContext;
    private SensorSet() {
    }

    private static SensorSet _instance = null;

    public static SensorSet getInstance() {
        if (_instance == null)
            _instance = new SensorSet();
        return _instance;
    }

    TextView vcp;
    TextView ph;
    TextView orp;
    EditText warningValue;
    public void setDataAndListen(Context context, View view) {

        if (mContext == null){
            mContext = context;

            vcp = view.findViewById(R.id.vcp_mean);
            ph = view.findViewById(R.id.ph_mean);
            orp = view.findViewById(R.id.orp_mean);

            warningValue = view.findViewById(R.id.warning_value);
            view.findViewById(R.id.sensor_save).setOnClickListener(view1 -> {
                float value = Float.parseFloat(warningValue.getText().toString());
                if (value > 100 || value < 0) {
                    ToastUtil.toast("设置失败，请设置值在0.0 ~ 100.0范围内");
                    return;
                }
                SharedPreferencesUtil.getInstance().putValue(Constant.warningValue, warningValue.getText());
            });
        }

        vcp.setText(String.valueOf(WaterQualityApplication.getApplication().getVCPAvg()));
        ph.setText(String.valueOf(WaterQualityApplication.getApplication().getPHAvg()));
        orp.setText(String.valueOf(WaterQualityApplication.getApplication().getORPAvg()));
        warningValue.setText(String.valueOf(SharedPreferencesUtil.getInstance().getValue(Constant.warningValue,"15.0")));
    }
}
